package cn.itcast.service.impl;


import cn.itcast.dao.UserDao;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.pojo.Menu;
import cn.itcast.pojo.Role;
import cn.itcast.pojo.User;
import cn.itcast.service.UserService;
import cn.itcast.utils.MD5Utils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
/*import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;*/
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class UserServiceImpl implements UserService  {

    @Autowired
    private UserDao userDao;

    /**
     * 保存用户基本信息
     *
     * @param user
     */
    @Override
    public void add(User user,Integer[] roleIds) {
        userDao.add(user);
        //关联保存角色信息
        if (roleIds != null) {
            Integer userId = user.getId();
            setUserAndRole(userId, roleIds);
        }
    }

    /**
     * 条件查询用户信息
     *
     * @param queryString
     * @return
     */
    @Transactional
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<User> page = userDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据用户id查询用户信息
     *
     * @param id
     * @return
     */
    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }

    /**
     * 查询指定用户关联的角色id
     *
     * @param roleId
     * @return
     */
    @Override
    public Integer[] selectedroleIdsId(Integer roleId) {
        return userDao.findRoleAssociationIds(roleId);
    }

    /**
     * 根据Id更新用户基本信息
     *
     * @param user
     */
    @Transactional
    @Override
    public void update(User user, Integer[] roleIds) {
        //更新用户基本信息
        userDao.update(user);
        //删除用户关联角色信息
        userDao.deleteRoleAssociation(user.getId());
        //设置用户关联角色信息
        setUserAndRole(user.getId(), roleIds);
    }

    /**
     * 根据id删除用户信息
     *
     * @param id
     */
    @Transactional
    @Override
    public void deleteById(Integer id) {
        //删除关联信息
        userDao.deleteRoleAssociation(id);
        //删除用户基本信息
        userDao.deleteById(id);
    }

    /**
     * 查询所有用户数据
     *
     * @return
     */
    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }

    /**
     * 设置用户和角色的关联信息
     *
     * @param userId
     * @param roleIds
     */
    private void setUserAndRole(Integer userId, Integer[] roleIds) {
        Stream.of(roleIds).forEach(roleId -> userDao.setRoleAssociation(userId, roleId));

        //for (Integer checkItemId : checkItemIds) {
        //    checkGroupDao.setCheckItemsAssociation(checkGroupId, checkItemId);
        //}
    }

}

